{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查阅文档\n",
    "\n",
    "受篇幅所限，本书无法对所有用到的tensorflow2.0函数和类一一详细介绍。读者可以查阅相关文档来做更深入的了解。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "print(tf.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.4.1 search for functions and classes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当我们想知道一个模块里面提供了哪些可以调用的函数和类的时候，可以使用`dir`函数。下面我们打印`dtypes`和`random`模块中所有的成员或属性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['DType',\n",
       " 'QUANTIZED_DTYPES',\n",
       " '__builtins__',\n",
       " '__cached__',\n",
       " '__doc__',\n",
       " '__file__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__path__',\n",
       " '__spec__',\n",
       " '_sys',\n",
       " 'as_dtype',\n",
       " 'bfloat16',\n",
       " 'bool',\n",
       " 'cast',\n",
       " 'complex',\n",
       " 'complex128',\n",
       " 'complex64',\n",
       " 'double',\n",
       " 'float16',\n",
       " 'float32',\n",
       " 'float64',\n",
       " 'half',\n",
       " 'int16',\n",
       " 'int32',\n",
       " 'int64',\n",
       " 'int8',\n",
       " 'qint16',\n",
       " 'qint32',\n",
       " 'qint8',\n",
       " 'quint16',\n",
       " 'quint8',\n",
       " 'resource',\n",
       " 'saturate_cast',\n",
       " 'string',\n",
       " 'uint16',\n",
       " 'uint32',\n",
       " 'uint64',\n",
       " 'uint8',\n",
       " 'variant']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(tf.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__builtins__',\n",
       " '__cached__',\n",
       " '__doc__',\n",
       " '__file__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__path__',\n",
       " '__spec__',\n",
       " '_sys',\n",
       " 'all_candidate_sampler',\n",
       " 'categorical',\n",
       " 'experimental',\n",
       " 'fixed_unigram_candidate_sampler',\n",
       " 'gamma',\n",
       " 'learned_unigram_candidate_sampler',\n",
       " 'log_uniform_candidate_sampler',\n",
       " 'normal',\n",
       " 'poisson',\n",
       " 'set_seed',\n",
       " 'shuffle',\n",
       " 'stateless_categorical',\n",
       " 'stateless_normal',\n",
       " 'stateless_truncated_normal',\n",
       " 'stateless_uniform',\n",
       " 'truncated_normal',\n",
       " 'uniform',\n",
       " 'uniform_candidate_sampler']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(tf.random)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通常我们可以忽略掉由`__`开头和结尾的函数（Python的特别对象）或者由`_`开头的函数（一般为内部函数）。通过其余成员的名字我们大致猜测出这个模块提供了各种随机数的生成方法，包括从均匀分布采样（`uniform`）、从正态分布采样（`normal`）、从泊松分布采样（`poisson`）等。\n",
    "\n",
    "## 2.4.2 use of functions\n",
    "\n",
    "想了解某个函数或者类的具体用法时，可以使用`help`函数。让我们以`ones`函数为例，查阅它的用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function ones in module tensorflow.python.ops.array_ops:\n",
      "\n",
      "ones(shape, dtype=tf.float32, name=None)\n",
      "    Creates a tensor with all elements set to 1.\n",
      "    \n",
      "    This operation returns a tensor of type `dtype` with shape `shape` and all\n",
      "    elements set to 1.\n",
      "    \n",
      "    For example:\n",
      "    \n",
      "    ```python\n",
      "    tf.ones([2, 3], tf.int32)  # [[1, 1, 1], [1, 1, 1]]\n",
      "    ```\n",
      "    \n",
      "    Args:\n",
      "      shape: A list of integers, a tuple of integers, or a 1-D `Tensor` of type\n",
      "        `int32`.\n",
      "      dtype: The type of an element in the resulting `Tensor`.\n",
      "      name: A name for the operation (optional).\n",
      "    \n",
      "    Returns:\n",
      "      A `Tensor` with all elements set to 1.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(tf.ones)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从文档信息我们了解到，ones函数会创建和输入tensor形状相同且元素为1的新tensor。我们可以验证一下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=2, shape=(2, 3), dtype=int32, numpy=\n",
       "array([[1, 1, 1],\n",
       "       [1, 1, 1]])>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.ones([2,3], tf.int32)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf2.0",
   "language": "python",
   "name": "tf2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
